package com.xunshangwang.pinhaohuo.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.xunshangwang.pinhaohuo.R;

import tool.library.widget.AutoCycleViewPager;


/**
 * Created by xzp on 16/1/18.
 */
public class CircleIndicator extends LinearLayout implements ViewPager.OnPageChangeListener{
    private int mIndicatorSize;
    private int mCheckedRes;
    private int mUncheckedRes;
    private int mIndicatorMargin;
    private ViewPager mViewPager;
    private int mCurrentPosition;
    private int mIndicatorCount;
    public CircleIndicator(Context context) {
        super(context);
        init(context,null);
    }

    public CircleIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public CircleIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    private void init(Context context,AttributeSet attrs){
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        mCurrentPosition=-1;
        mIndicatorCount=0;
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.CircleIndicator);
        mIndicatorSize=array.getDimensionPixelSize(R.styleable.CircleIndicator_indicator_size,0);
        mCheckedRes=array.getResourceId(R.styleable.CircleIndicator_checked_drawable,-1);
        mUncheckedRes=array.getResourceId(R.styleable.CircleIndicator_unchecked_drawable,-1);
        mIndicatorMargin=array.getDimensionPixelOffset(R.styleable.CircleIndicator_indicator_margin,0);
        array.recycle();
    }
    public void setViewPager(ViewPager viewPager){
        mViewPager=viewPager;
        viewPager.removeOnPageChangeListener(this);
        viewPager.addOnPageChangeListener(this);
        notifyDataSetChanged();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        check(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void notifyDataSetChanged(){
        if(mViewPager==null)
            return;
        PagerAdapter adapter=mViewPager.getAdapter();
        if(adapter==null)
            return;
        if(adapter instanceof AutoCycleViewPager.CyclePagerAdapter){
            AutoCycleViewPager.CyclePagerAdapter cycleAdapter= (AutoCycleViewPager.CyclePagerAdapter) adapter;
            mIndicatorCount=cycleAdapter.getSize();
        }
        else
            mIndicatorCount=adapter.getCount();
        updateChild(mIndicatorCount);
        check(mViewPager.getCurrentItem());
        if(mIndicatorCount<2)
            this.setVisibility(GONE);
        else
            this.setVisibility(VISIBLE);
    }
    private void check(int position){
        if(mIndicatorCount==0)
            return;
        position=position%mIndicatorCount;
        if(mCurrentPosition==position)
            return;
        if(position<0)
            return;
        View checkedChild=getChildAt(position);
        if(mCheckedRes>0)
            checkedChild.setBackgroundResource(mCheckedRes);
        else
            checkedChild.setBackgroundColor(Color.BLACK);
        if(mCurrentPosition>=0 && mCurrentPosition<getChildCount()){
            View uncheckedChild=getChildAt(mCurrentPosition);
            if(mUncheckedRes>0)
                uncheckedChild.setBackgroundResource(mUncheckedRes);
            else
                uncheckedChild.setBackgroundColor(Color.WHITE);
        }
        mCurrentPosition=position;
    }
    public void updateChild(int num)
    {
        int childNum=getChildCount();
        if(childNum<num)
        {
            for(int i=childNum;i<num;i++)
            {
                View child=new View(this.getContext());
                LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(mIndicatorSize,mIndicatorSize);
                params.setMargins(mIndicatorMargin, 0, mIndicatorMargin, 0);
                if(mUncheckedRes>0)
                    child.setBackgroundResource(mUncheckedRes);
                else
                    child.setBackgroundColor(Color.WHITE);
                this.addView(child,params);
            }
        }
        else if(childNum>num)
        {
            for(int i=childNum-1;i>=num;i--)
                this.removeView(getChildAt(i));
        }
    }
}
